package richbar.com.richbar.activity.product;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.adapter.home.HomeRecommendAdapter;
import richbar.com.richbar.adapter.product.NavAdapter;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.CollectDelResult;
import richbar.com.richbar.bean.CollectResult;
import richbar.com.richbar.bean.GoodsBean;
import richbar.com.richbar.bean.ShopResult;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.BannerLayout;
import richbar.com.richbar.view.NoScrollGridView;
import richbar.com.richbar.view.pop.ViewTopListPop;

/**
 * Created by QiYang on 2018/12/10.
 */

public class ShopAc extends BaseActivity {
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.classify)
    TextView classify;
    @BindView(R.id.input_et)
    TextView input_et;
    ImageView pic;
    TextView name;
    TextView num;
    TextView fouce;
    BannerLayout bannerLayout;
    NoScrollGridView gridView;
    private HomeRecommendAdapter recommendAdapter;
    private NavAdapter navAdapter;
    private String id;
    private String seller_collect_id;
    ShopResult.DataEntity dataEntity;
    private boolean isCollect;
    private LoadingDialog loadingDialog;

    @Override
    public int initLayout() {
        return R.layout.ac_shop;
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {
        loadingDialog = new LoadingDialog(this,0);
        ButterKnife.bind(this);
        recommendAdapter = new HomeRecommendAdapter(R.layout.item_home_page, new ArrayList<GoodsBean>());
        RecycleViewHelp.setGridMannager(recycleView, 0,2,this);
        recycleView.setAdapter(recommendAdapter);
        View topView = LayoutInflater.from(this).inflate(R.layout.ac_shop_top, null);
        recommendAdapter.addHeaderView(topView);
        pic = topView.findViewById(R.id.pic);
        name = topView.findViewById(R.id.name);
        num = topView.findViewById(R.id.num);
        fouce = topView.findViewById(R.id.fouce);
        bannerLayout = topView.findViewById(R.id.banner);
        gridView = topView.findViewById(R.id.gridView);
    }

    @Override
    public void initData() {
        id = getBundle().getString("id");
        getData();
    }

    private void getData() {
        NetWorkUtil.getShopHome(id, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final ShopResult result = MyGson.getInstance().fromJson(data, ShopResult.class);
                if (checkIsOk(result)) {
                    setData(result);
                }
            }
        });
    }

    private void setData(ShopResult result) {
        dataEntity = result.getData();
        seller_collect_id = dataEntity.getColl_id();
        recommendAdapter.setNewData(dataEntity.getRecommend());
        name.setText(dataEntity.getSeller_name());
        num.setText(dataEntity.getSeller_collect_num() + "关注");
        GlideUtil.loadImage(this,NetWorkUtil.URL + dataEntity.getSeller_logo(),pic);
        isCollect = dataEntity.getIssub() == 1 ? false : true;
        fouce.setText(isCollect?"已关注":"关注");
        fouce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCollect){
                    unCollect();
                    LogUtil.e("PPPPPPP",1);
                }else {
                    collect();
                    LogUtil.e("PPPPPPP",2);
                }
            }
        });
        getBanner();
        getNav();
    }

    private void getNav() {
        navAdapter = new NavAdapter(this, dataEntity.getNavlist());
        gridView.setAdapter(navAdapter);
    }

    private void getBanner() {
        List<String> urls = new ArrayList<>();
        List<ShopResult.DataEntity.Banner> list_banner = dataEntity.getBanner();
        if (list_banner.size() > 0) {
            for (int i = 0; i < list_banner.size(); i++) {
                urls.add(NetWorkUtil.URL + list_banner.get(i).getImg());
            }
            bannerLayout.setViewUrls(urls);
            bannerLayout.setOnBannerChangeListener(new BannerLayout.OnBannerChangeListener() {
                @Override
                public void onChange(int position) {

                }
            });
            bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {

                }
            });
        }

    }


    @OnClick({R.id.close, R.id.input_body, R.id.share,R.id.shop_intro, R.id.classify, R.id.link})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.input_body:
                start(ProductListAc.class, new BaseActivity.OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("goodsName", StringUtil.getStr(input_et));
                        return bundle;
                    }
                });
                break;
            case R.id.share:
                break;
            case R.id.shop_intro:
                start(ShopIntroAc.class, new OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("id",id);
                        return bundle;
                    }
                });
                break;
            case R.id.classify:
                showClassify();
                break;
            case R.id.link:
                break;
        }
    }


    /**
     * 收藏
     */
    private void collect() {
        loadingDialog.show();
        NetWorkUtil.addCollect(BaseApplication.uid, "2", id, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final CollectResult result = MyGson.getInstance().fromJson(data, CollectResult.class);
                if (checkIsOk(result)) {
                    loadingDialog.dismiss();
                    if (Integer.valueOf(result.getData().getColl_id()) > 0) {
                        ToastUtil.showToast(ShopAc.this, "收藏成功");
                        fouce.setText("已关注");
                        seller_collect_id = result.getData().getColl_id();
                        isCollect = true;
                    } else {
                        ToastUtil.showToast(ShopAc.this, "收藏失败");
                    }

                }
            }
        });
    }

    private void unCollect(){
        loadingDialog.show();
        NetWorkUtil.delCollect("2",seller_collect_id, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final CollectDelResult result = MyGson.getInstance().fromJson(data, CollectDelResult.class);
                if (checkIsOk(result)){
                    loadingDialog.dismiss();
                    if (result.getData().getResult() == 1){
                        ToastUtil.showToast(ShopAc.this,"删除成功");
                        fouce.setText("关注");
                        isCollect = false;
                    }else {
                        ToastUtil.showToast(ShopAc.this,"删除失败");
                    }
                }
            }
        });
    }

    private void showClassify() {
        ViewTopListPop pop = new ViewTopListPop(this);
        pop.show(classify, dataEntity.getHotcate(), new ViewTopListPop.Callback() {
            @Override
            public void callback(final ShopResult.DataEntity.HotCate bean) {
                start(ProductDetailsAc.class, new OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("goodsName",bean.getName());
                        return bundle;
                    }
                });
            }
        });
    }

}
